<template>
  <template v-for="subItem in menuList" :key="subItem.name">
    <ElSubMenu v-if="subItem.children?.length" :index="subItem.name">
      <template #title>
        <ElIcon>
          <Icon :icon="subItem.meta.icon"></Icon>
        </ElIcon>
        <span class="sle">{{ subItem.meta.title }}</span>
      </template>
      <SubMenu :menu-list="subItem.children" />
    </ElSubMenu>
    <ElMenuItem v-else :index="subItem.name" @click="handleClickMenu(subItem)">
      <ElIcon>
        <Icon :icon="subItem.meta.icon"></Icon>
      </ElIcon>
      <template #title>
        <span class="sle">{{ subItem.meta.title }}</span>
      </template>
    </ElMenuItem>
  </template>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { ElIcon, ElSubMenu, ElMenuItem } from "element-plus";
import { Icon } from "@/components/Icon";
import { urlParamToJson } from "@/utils";
defineProps<{ menuList: Menu.MenuOptions[] }>();
const router = useRouter();
const handleClickMenu = (subItem: Menu.MenuOptions) => {
  if (subItem.meta.isLink) {
    if (subItem.meta.param) {
      return window.open(subItem.path + "?" + subItem.meta.param, "_blank");
    } else {
      return window.open(subItem.path as string, "_blank");
    }
  } else {
    if (subItem.meta.param) {
      // 根据名称判断，防止path重复
      router.push({ name: subItem.name, query: urlParamToJson(subItem.meta.param) });
    } else {
      router.push({ name: subItem.name });
    }
  }
};
</script>

<style lang="scss">
.el-sub-menu .el-sub-menu__title:hover {
  color: var(--el-menu-hover-text-color) !important;
  background-color: transparent !important;
}
.el-menu--collapse {
  .is-active {
    .el-sub-menu__title {
      color: #ffffff !important;
      background-color: var(--el-color-primary) !important;
    }
  }
}
.el-menu-item {
  &:hover {
    color: var(--el-menu-hover-text-color);
  }
  &.is-active {
    color: var(--el-menu-active-color) !important;
    background-color: var(--el-menu-active-bg-color) !important;
    &::before {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 4px;
      content: "";
      background-color: var(--el-color-primary);
    }
  }
}
.vertical,
.classic,
.transverse {
  .el-menu-item {
    &.is-active {
      &::before {
        left: 0;
      }
    }
  }
}
.columns {
  .el-menu-item {
    &.is-active {
      &::before {
        right: 0;
      }
    }
  }
}
.classic,
.transverse {
  #driver-highlighted-element-stage {
    background-color: #606266 !important;
  }
}
</style>
